// pages/operation/clueManage/clueManageIndex/index.js
import { ClueModel } from "../../../../api/api-work/clueModel.js"
import Dialog from '@vant/weapp/dialog/dialog';
const clueModel = new ClueModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab: "first",
    mine: '0', // 0:全部线索 1:我的线索
    allType: "4", // 默认全部 5：回收站
    stage: "1", //
    allTypeOptions: [
      { text: '全部', value: '4' },
      { text: '本月新增', value: '2' },
      { text: '本周新增', value: '1' },
      { text: '今日新增', value: '3' },
    ],
    wxParam: "",
    allPageNum: 1,
    allTotal: 0,
    allClueTableData: [],
    allCluetriggered: false,
    allLoadStatus: "",
    selectId: "",
    isShowDispense: false, // 分配跟进人弹窗
    dispensePersons: [], // 跟进人列表
    selectDispenseId: "", // 选中的跟进人id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // await this.getAllClueDatas();
  },

  /**
  * 生命周期函数--监听页面显示
  */
  async onShow() {
    this.setData({
      allPageNum: 1,
      allClueTableData: []
    })
    await this.getAllClueDatas();
  },

  // tab切换
  onTabChange(e) {
    if (e.detail.name == "first") {
      // 全部线索
      this.setData({
        mine: '0',
        allType: "4",
        stage: "1",
        allPageNum: 1,
        allClueTableData: []
      })

    } else if (e.detail.name == "second") {
      this.setData({
        mine: "1",
        allType: "4",
        stage: "1",
        allPageNum: 1,
        allClueTableData: []
      })
      // 我的线索
    } else {
      // 回收站
      this.setData({
        mine: '0',
        stage: "",
        allType: "5",
        allPageNum: 1,
        allClueTableData: []
      })
    }
    this.getAllClueDatas()
  },

  searchClue() {
    this.setData({
      allPageNum: 1,
      allClueTableData: []
    })
    this.getAllClueDatas();
  },

  // 获取全部线索列表
  async getAllClueDatas() {
    clueModel.getClueListApi({ wxParam: this.data.wxParam, pageNum: this.data.allPageNum, pageSize: 10, stage: this.data.stage, type: this.data.allType, mine: this.data.mine }).then(res => {
      let rows = this.data.allClueTableData;
      rows.push(...res.rows)
      this.setData({
        allClueTableData: rows,
        allTotal: res.total
      })
      if (this.data.allTotal == 0) {
        this.setData({
          // 无数据
          allLoadStatus: ""
        })
      } else if (this.data.allClueTableData.length < this.data.allTotal) {
        this.setData({
          allLoadStatus: "loadmore"
        })
      } else {
        this.setData({
          allLoadStatus: "nomore"
        })
      }
    })
  },

  // 改变全部线索
  changeAllType(val) {
    this.setData({
      allPageNum: 1,
      allType: val.detail,
      allClueTableData: []
    })
    this.getAllClueDatas();
  },

  // 全部线索上拉加载
  getMoreAllClue() {
    if (this.data.allLoadStatus === "nomore") {
      return
    }
    this.setData({
      allPageNum: this.data.allPageNum + 1,
      allLoadStatus: "loading"
    })
    this.getAllClueDatas();
  },

  // 全部线索下拉刷新
  refreshMoreAllClue() {
    this.setData({
      allCluetriggered: false,
      allPageNum: 1,
      allClueTableData: []
    })
    this.getAllClueDatas();
  },

  // 添加线索
  addClue() {
    wx.navigateTo({
      url: '/pages/operation/clueManage/addClue/index',
    })
  },

  // 删除线索
  deleteClue(e) {
    const query = e.currentTarget.dataset.query;
    Dialog.confirm({
      title: '确认删除？',
    })
      .then(() => {
        // on confirm
        if (this.data.allType == '5') {
          // 回收站删除
          clueModel.deleteClueApi({ id: query.id, type: 2 }).then(res => {
            this.searchClue()
          })
        } else {
          // 其他删除
          clueModel.deleteClueApi({ id: query.id, type: 1 }).then(res => {
            this.searchClue()
          })
        }

      })
      .catch(() => {
        // on cancel
      });
  },

  // 还原线索
  recycleClue(e) {
    const query = e.currentTarget.dataset.query;
    Dialog.confirm({
      title: '确认还原？',
    })
      .then(() => {
        // on confirm
        // 还原
        const param = { id: query.id, type: 3, ...(query.recordId && { recordId: query.recordId }) }
        console.log(param);
        clueModel.deleteClueApi(param).then(res => {
          this.searchClue()
        })
      })
      .catch(() => {
        // on cancel
      });
  },

  // 分配跟进人
  dispensePerson(e) {
    const query = e.currentTarget.dataset.query;
    // dispensePersons
    clueModel.getPeopleListApi({ pageNum: 1, pageSize: 999 }).then(res => {
      this.setData({
        selectId: query.id,
        isShowDispense: true,
        dispensePersons: res.rows
      })
    })
  },

  // 选中跟进人
  selectDispense(e) {
    const query = e.currentTarget.dataset.query;
    this.setData({
      selectDispenseId: query.id
    })
  },

  // 确定跟进人
  confirmDispense() {
    clueModel.saveCounselorApi({ ids: [this.data.selectId], counselorId: this.data.selectDispenseId }).then(res => {
      this.setData({
        isShowDispense: false
      })
      this.searchClue()
    })
  },

  onCloseDispense() {
    this.setData({
      isShowDispense: false
    })
  },

  // 线索详情
  tapClue(e) {
    console.log("线索")
    const data = JSON.stringify(e.currentTarget.dataset.query);
    wx.navigateTo({
      url: `/pages/operation/clueManage/clueDetail/index?obj=${data}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})